package com.tanhua.listener;

import com.tanhua.domain.VideoScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-video",consumerGroup = "tanhua-video-group")
public class VideoScoreListener implements RocketMQListener<Map>{
@Autowired
private MongoTemplate mongoTemplate;
    @Override
    public void onMessage(Map map) {
        long userId=Long.parseLong(map.get("userId").toString());
        long vid=Long.parseLong(map.get("vid").toString());
        System.out.println("vid:"+vid);
        int type=Integer.parseInt(map.get("type").toString());

        //封装数据
        VideoScore videoScore=new VideoScore();
        videoScore.setDate(new Date().getTime());
        videoScore.setId(new ObjectId());
        videoScore.setUserId(userId);
        videoScore.setVideoId(vid);

        /*- 操作1： 小视屏的发布 +2
        - 操作2： 作者关注 +100
        - 操作3： 取消作者关注 -100
        - 操作4： 小视频点赞 + 5
        - 操作5：小视频取消点赞 -5
        - 操作6：发布评论 +10*/
        Double score=0D;
        switch (type){
            case 1:{
                score = 2D;
                break;
            }case 2:{
                score = 100D;
                break;
            }case 3:{
                score = -100D;
                break;
            }case 4:{
                score = 5D;
                break;
            }case 5:{
                score = -5D;
                break;
            }case 6:{
                score = 10D;
                break;
            }default:{
                break;
            }

        }
        videoScore.setScore(score);
        System.out.println(videoScore);
        //保存到mongodb中
        mongoTemplate.save(videoScore);
        System.out.println("历史行为数据统计到mongodb中了");
    }
}
